package com.app.controller;

import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Controller
@RequestMapping("/role")
public class PersonPositionController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private PersonService personService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private PersonPositionService personPositionService;

    @Autowired
    private PersonResumeService personResumeService;
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/getPositionList")//角色管理分页查询
    public Result<Object> getPositionList(
            @RequestParam("personId") int personId,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        List<PersonPositionModel> positionModelList = personPositionService.getPositionList(personId);
        if (positionModelList == null) {
            return Result.success(null);
        }
        return Result.success(positionModelList);
    }

    @PostMapping("/insertPosition")//新增/修改职务
    public Result<Object> insertPosition(
            @RequestParam("id") Integer id,
            @RequestParam("departmentId") Integer departmentId,
            @RequestParam("personId") Integer personId,
            @RequestParam("position") String position,
            @RequestParam("isMajor") Integer isMajor,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        PersonPositionModel model = new PersonPositionModel();
        model.setDepartmentId(departmentId);
        model.setPersonId(personId);
        model.setPosition(position);
        model.setIsMajor(isMajor);
        try {
            if (id > 0) {
                model.setId(id);
                model.setUpdateTime(CommonHelp.getNowTimestamp());
                personPositionService.update(model);
            } else {
                model.setCreateTime(CommonHelp.getNowTimestamp());
                personPositionService.insert(model);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
        return Result.success(null);
    }

    @GetMapping("/deletePositionById")//删除职务
    public Result<Object> deleteById(
            @RequestParam("id") Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        PersonPositionModel positionModel = personPositionService.selectPosById(id);
        if (positionModel == null) {
            return Result.error(500, "该职务不存在");
        } else {
            personPositionService.delete(id);
            return Result.success(null);
        }
    }
}
